<?php

class ClientsDAO{
    
    public function ClientsDAO(){  
    
    }
    
	/**
     * @param Client $client
     * @return true if the excecution was successful, false if opposite
     */
    public function addClient($client){
        $bd = new Bd();
        
        $sql = "INSERT INTO clients (idType, clientId, name, lastName, address, phone, email, access) 
        		VALUES ('".$client->getIdType()."', ".$client->getClientId().", '".$client->getName()."', '".$client->getLastName()."', '".$client->getAddress()."', '".$client->getPhone()."', '".$client->getEmail()."', 't');";
        
        return $bd->doUpdate($sql);
    }
    
    /**
     * @param int $idClient > 0
     * @return array if the client exists, false otherwise
     */
    public function getClient($idClient){
        if($this->existClient($idClient)){
            $bd = new Bd();        
            $sql = "SELECT * FROM clients WHERE id = ".$idClient.";";            
            return $bd->doQueryRow($sql);
        }
        return false;
    }
    
    /**
     * @param int $idClient > 0
     * @return true if the client exists, false otherwise
     */
    public function existClient($idClient){
        $bd = new Bd();
        
        $sql = "SELECT COUNT(*) FROM clients WHERE id = ".$idClient.";";
        
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }
    
    /**
     * @param int $idClient > 0
     * @return true if the client could be deactivated, false otherwise or if it wasn't active
     */
    public function deactivateClient($idClient){
        if($this->isClientActive($idClient)){
            $bd = new Bd();        
            $sql = "UPDATE clients SET access = 'f' WHERE id = ".$idClient.";";            
            return $bd->doUpdate($sql);
        }
        return false;
    }
    
    /**
     * @param int $idClient > 0
     * @return true if the client could be activated, false otherwise or if it wasn't inactive
     */
    public function activateClient($idClient){
        if(!$this->isClientActive($idClient)){
            $bd = new Bd();        
            $sql = "UPDATE clients SET access = 't' WHERE id = ".$idClient.";";            
            return $bd->doUpdate($sql);
        }
        return false;
    }

    /**
     * @param int $idClient > 0
     * @return true if the client is active, false otherwise or if it doesn't exist
     */
    public function isClientActive($idClient){
        if($this->existClient($idClient)){
            $bd = new Bd();        
            $sql = "SELECT COUNT(*) FROM clients WHERE id = ".$idClient." and access = 't';";            
            if ($bd->doQueryOne($sql) > 0)
                return true;
            else return false;
        }
        return false;
    }
    
    /**
     * @return array with all existing clients
     */
    public function getClients(){
        $bd = new Bd();        
        $sql = "SELECT * FROM clients;";            
        return $bd->doQueryAll($sql);
    }
    
    /**
     * @return array with all existing clients
     */
    public function searchClient($id){
        $bd = new Bd();        
        $sql = "SELECT * FROM clients WHERE clientId = ".$id." ;";
        return $bd->doQueryAll($sql);
    }
    
    /**
     * @param string $email valid email
     * @return true if found false otherwise
     */
    public function existUserEmail($email){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM clients WHERE email = '".$email."' ;";
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }
    
    /**
     * @param string $username valid userName
     * @return true if found false otherwise
     */
    public function existUserName($username){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM clients WHERE userName = '".$username."' ;";
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }
    
    /**
     * @param string $id valid id
     * @return true if found false otherwise
     */
    public function existUserId($type, $id){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM clients WHERE idType = ".$type." and clientId = ".$id." ;";
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }    

    /**
     * @return int >= 0 with the matching number of clients
     */
    public function howManyClients(){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM clients;";
        return $bd->doQueryOne($sql);
    }
    
}

?>